package logic

import (
	"context"
	"fmt"
	"math/rand"
	"tieba/models/cache"
	"tieba/tiebarpc/internal/svc"
	"tieba/tiebarpc/tiebarpc"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *tiebarpc.SendSmsRequest) (*tiebarpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line
	get, _ := cache.Rdb.Get(cache.Ctx, "sendNum"+in.Mobile).Int()
	if get >= 1 {
		return nil, fmt.Errorf("当前一分钟内发送过一次")
	}
	code := rand.Intn(9000) + 1000
	//sms, err := pkg.SendSms(in.Mobile, strconv.Itoa(code))
	//if err != nil {
	//	return nil, err
	//}
	//if *sms.Body.Code != "OK" {
	//	return nil, fmt.Errorf(*sms.Body.Message)
	//}
	cache.Rdb.Set(cache.Ctx, "send"+in.Mobile, code, 0)
	cache.Rdb.Incr(cache.Ctx, "sendNum"+in.Mobile)
	cache.Rdb.Expire(cache.Ctx, "sendNum"+in.Mobile, time.Minute*1)
	return &tiebarpc.SendSmsResponse{}, nil
}
